/**
 * Project Name:lee
 * File Name:ResourcesController.java
 * Package Name:com.lee.controller.system
 * Date:2017年1月4日下午3:01:08
 * Copyright (c) 2017, chenzhou1025@126.com All Rights Reserved.
 *
*/

package com.lee.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lee.annotation.SystemLog;
import com.lee.base.controller.BaseController;
import com.lee.common.page.PageView;
import com.lee.common.utils.Common;
import com.lee.common.utils.JsonUtils;
import com.lee.common.utils.TreeObject;
import com.lee.common.utils.TreeUtil;
import com.lee.system.model.ResourcesInfo;
import com.lee.system.service.ResourcesService;
import com.lee.system.service.UserService;

/**
 * ClassName:ResourcesController <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2017年1月4日 下午3:01:08 <br/>
 * @author   dell
 * @version  
 * @since    JDK 1.6
 * @see 	 
 */
@Controller
@RequestMapping("/resources/")
public class ResourcesController extends BaseController {
	@Autowired
	private ResourcesService resourcesService;
	@Autowired
	private UserService userService;
	//to列表
	@RequestMapping("intoResourcesList")
	public ModelAndView intoResourcesList(){
		int id =Integer.parseInt(getPara("id"));
		ModelAndView mav = new ModelAndView("/system/resources/list");
		mav.addObject("res", findButton(id));
		return mav;
	}
	//分页查询菜单
	@ResponseBody
	@RequestMapping("findByPage")
	public PageView findByPage(ResourcesInfo resourcesInfo, String pageNow,
			String pageSize,String column,String sort) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map = toFormMap(pageNow, pageSize);
		map.put("column", column);
		map.put("sort", sort);
		map.put("type", "1");
		resourcesInfo.setType("1");
		List<ResourcesInfo> list = resourcesService.findByPage(map);
		int count = resourcesService.count(resourcesInfo);
		pageView.setRecords(list);
		pageView.setRowCount(count);
        return pageView;
	}
	//查询所有的菜单
	@ResponseBody
	@RequestMapping("getResourcesAll")
	public List<Map<String, Object>> getResourcesAll(){
		List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
		Map<String, Object> resQueryForm = new HashMap<String, Object>();
		resQueryForm.put("type", "1"); //type(1代表菜单，2代表按钮)
		List<ResourcesInfo> list = resourcesService.findAll(resQueryForm);
		for (ResourcesInfo resourcesInfo : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", resourcesInfo.getName());
			map.put("id", resourcesInfo.getId());
			lists.add(map);
		}
		return lists;
	}
	//查询用户所有的权限
	@ResponseBody
	@RequestMapping("getResourcesByUserId")
	public Map<String, Object> getResourcesByUserId(String id){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", Integer.parseInt(id));
		List<ResourcesInfo> resourcesInfos = resourcesService.findRes(map);
		map.put("res", resourcesInfos);
		return map;
	}
	
	
	//添加
	@SystemLog(module="系统管理",methods="添加菜单")
	@RequestMapping("add")
	@ResponseBody
	public String add(HttpServletRequest request,ResourcesInfo resourcesInfo){
		String flag="false";
		int i = resourcesService.insert(resourcesInfo);
		if(i>0){
			flag = "true";
		}
		return flag;
	}
	//删除
	@SystemLog(module="系统管理",methods="删除菜单")
	@RequestMapping("del")
	@ResponseBody
	public String del(HttpServletRequest request,int id){
		String flag="false";
		//true代表真删除，false代表假删除
		int i = resourcesService.delete(id,true);
		if(i>0){
			flag = "true";
		}
		return flag;
	}
	//批量删除
	@SystemLog(module="系统管理",methods="批量删除菜单")
	@RequestMapping("delMore")
	@ResponseBody
	public Map<String, Object> delMore(HttpServletRequest request){
		String[] ids = getParaValues("id");
		Map<String, Object> map = new HashMap<String, Object>();
		//true代表真删除，false代表假删除
		int count = 0;
		try{
			int i = 0;
			for (String id : ids) {
				i = resourcesService.delete(Integer.parseInt(id),true);
				if(i>0){
					count++;
				}
			}
		}catch(Exception e){
			e.getStackTrace();
		}
		map.put("sucCount", count);
		map.put("faiCount", (ids.length-count));
		return map;
	}
	
	@RequestMapping("toPermission")
	public ModelAndView toPermission(HttpServletRequest request,String id){
		ModelAndView mav = new ModelAndView("/system/user/permission");
		Map<String, Object> map = new HashMap<String, Object>();
		List<ResourcesInfo> resourcesInfos = resourcesService.findAll(map);
		List<TreeObject> list = new ArrayList<TreeObject>();
		for (ResourcesInfo resourcesInfo2 : resourcesInfos) {
			TreeObject ts = new TreeObject();
			String reString = JsonUtils.objectToJson(resourcesInfo2);
			Map<String,Object> map2 = JsonUtils.parseJSONMap(reString);
			Common.flushObject(ts, map2);
			list.add(ts);
		}
		TreeUtil treeUtil = new TreeUtil();
		List<TreeObject> ns = treeUtil.getChildTreeObjects(list, 0);
		mav.addObject("permissions", ns);
		return mav;
	}
	
	@SystemLog(module="系统管理",methods="分配权限")
	@ResponseBody
	@RequestMapping("addUserRes")
	public Map<String, Object> addUserRes(String userId,String ids,HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		map = userService.addUserRes(userId, ids);
		return map;
	}
}

